/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.benimetum.managers;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 *
 * @author sony
 */
public class XMLParse_For_CreateCourseMethod {
       public static void main(String argv[]){
       String courseID = null;
       String courseName = null;
       String sectionCode = null;
       String teacherID = null;
       
       try{
           File fXmlFile = new File("ders9.xml");
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(fXmlFile);
		doc.getDocumentElement().normalize();
                
                NodeList nList = doc.getElementsByTagName("Ders");
		
               for (int temp = 0; temp < nList.getLength(); temp++) {
 
		   Node nNode = nList.item(temp);
		   String [] studentIDs=null;
		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
		      Element eElement = (Element) nNode;
 
		      
                      courseID = getTagValue("Kod", eElement);
                      courseName = getTagValue("DersAd", eElement);
                      sectionCode = getTagValue("subeKod", eElement);
	              teacherID = "t"+getTagValue("ogretmenID", eElement);
                 
                      NodeList nList2 = doc.getElementsByTagName("ogrenciler"+courseID+sectionCode);
                      studentIDs = new String[nList2.getLength()];
                      for(int temp2=0; temp2 < nList2.getLength(); temp2++){
                            Node nNode2 = nList2.item(temp2);
                            if(nNode2.getNodeType()==Node.ELEMENT_NODE){
                            Element eElement2 = (Element) nNode2;     
                            studentIDs[temp2]="st"+getTagValue("no", eElement2);
                            }
                        }  
                      }
                   CourseManager.createCourse(courseID, courseName, teacherID, studentIDs);
                }
       }
       catch (Exception e) {
	  }
    
    
    
    }

    private static String getTagValue(String sTag, Element eElement){
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        return nValue.getNodeValue();
       
    }
}
